package com.flx.dis.registry.zk.controller;

import com.flx.common.utils.result.ResultResponse;
import com.flx.common.utils.validate.ValidateUtils;
import com.flx.dis.registry.zk.entity.UserDO;
import com.flx.dis.registry.zk.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Fenglixiong
 * @Create 2022/4/19 0:39
 * @Description
 **/
@Api
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get/{id}")
    public ResultResponse get(@PathVariable("id")Long id){
        try{
            return ResultResponse.success(userService.get(id));
        }catch (Exception e){
            return ResultResponse.error("请求失败！");
        }
    }

    @PostMapping("/add")
    public ResultResponse add(@RequestBody UserDO userDO){

        return ValidateUtils.validate(userDO,()-> {
            try {
                return ResultResponse.success(userService.add(userDO));
            } catch (Exception e) {
                return ResultResponse.error("请求错误!");
            }
        });
    }

    @PostMapping("/update")
    public ResultResponse update(@RequestBody UserDO userDO){
        try{
            return ResultResponse.success(userService.update(userDO));
        }catch (Exception e){
            return ResultResponse.error("请求失败！");
        }
    }

    @GetMapping("/delete/{id}")
    public ResultResponse delete(@PathVariable("id")Long id){
        try{
            return ResultResponse.success(userService.delete(id));
        }catch (Exception e){
            return ResultResponse.error("请求失败！");
        }
    }

}
